import { existsSync, mkdirSync, writeFileSync } from 'fs';
import { dirname, join } from 'path';
import { IMessage } from 'qq-guild-bot';
import { stringify } from 'yaml';
import { readYaml } from '../../../src/lib/tool';
let context = {}; //添加图片上下文
let contextTimer = {}; //记时

export async function add(e: { eventType: string; msg: IMessage }) {
  // 私聊禁用
  if (e.eventType === 'DIRECT_MESSAGE_CREATE') {
    return;
  }

  // 1	全体成员
  // 2	管理员
  // 4	群主/创建者
  // 5	子频道管理员
  // 没有权限
  if (
    !e.msg.member.roles.includes('4')
    // && !e.msg.member.roles.includes('5')
  ) {
    let message = {
      content: `<@!${e.msg.author.id}> 无权限`,
      msg_id: e.msg.id
    };
    await client.messageApi.postMessage(e.msg.channel_id, message);

    return true;
  }

  let msg = e.msg.content.replace('/添加', '').replace(`<@!${bot.id}>`, '').trim();
  if (msg === '') {
    let message = {
      content: `<@!${e.msg.author.id}> 内容不能为空!`,
      msg_id: e.msg.id
    };
    await client.messageApi.postMessage(e.msg.channel_id, message);
    return true;
  }
  let message = {
    content: `<@!${e.msg.author.id}> 请发送内容!`,
    msg_id: e.msg.id
  };

  //上下文添加
  context[e.msg.author.id] = {
    text: msg
  };
  await client.messageApi.postMessage(e.msg.channel_id, message);

  contextTimer[e.msg.author.id] = setTimeout(async () => {
    if (context[e.msg.author.id]) {
      delete context[e.msg.author.id];
      let message = {
        content: `<@!${e.msg.author.id}> 添加已取消!`,
        msg_id: e.msg.id
      };
      await client.messageApi.postMessage(e.msg.channel_id, message);
    }
  }, 120000);

  return true;
}

export async function addContent(e: { eventType: string; msg: IMessage }) {
  if (e.eventType === 'MESSAGE_CREATE') {
    if (!context[e.msg.author.id]) {
      return;
    }

    if (e.msg.attachments && e.msg.attachments.length > 1) {
      let message = {
        content: `<@!${e.msg.author.id}> 只能用一张图片哦!`,
        msg_id: e.msg.id
      };
      await client.messageApi.postMessage(e.msg.channel_id, message);
      clearTimeout(contextTimer[e.msg.author.id]);
      delete contextTimer[e.msg.author.id];
      delete context[e.msg.author.id];
      return true;
    }
    let bqPath = join(process.cwd(), '/plugins/biaoqing/resources/biaoqing/biaoqing.yaml');

    if (!existsSync(bqPath)) {
      mkdirSync(dirname(bqPath), { recursive: true });
    }

    let bq = readYaml(bqPath);

    if ((bq ?? '') === '' || typeof bq === 'string') {
      bq = {};
    }

    bq[context[e.msg.author.id].text] = {
      content: e.msg.content ? e.msg.content : undefined,
      image: e.msg.attachments ? e.msg.attachments[0].url : undefined
    };

    writeFileSync(bqPath, stringify(bq));

    let message = {
      content: `<@!${e.msg.author.id}> 添加成功!`,
      msg_id: e.msg.id
    };
    await client.messageApi.postMessage(e.msg.channel_id, message);

    clearTimeout(contextTimer[e.msg.author.id]);
    delete contextTimer[e.msg.author.id];
    delete context[e.msg.author.id];
    return true;
  }
}
